import java.util.HashMap;
import java.util.Map;

import org.cemantika.exception.KnowledgeSessionNotCreatedException;
import org.cemantika.management.ContextManager;
import org.cemantika.management.ContextualBehavior;
import org.cemantika.management.ContextualManipulation;
import org.cemantika.management.drools.DroolsContextualManipulation;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.financial.management.contextual.focus.LancamentoCreate;
import com.financial.management.entities.Categoria;
import com.financial.management.entities.Lancamento;
import com.financial.management.service.LancamentoServiceImp;



public class MainRuleBased {

	public static void main(String[] args) {

		ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("file:WebContent/WEB-INF/applicationContext.xml");
        LancamentoServiceImp lanc = (LancamentoServiceImp)context.getBean("lancamentoServiceImp");
        Lancamento l =  lanc.getById((long)1);
        l.setValor(1213.0);
	    context.close();
		
		DroolsContextualManipulation dcm = new DroolsContextualManipulation();
		dcm.setStatefulSession(createStatefulSession());
		Map<Integer, ContextualManipulation> mapdcm = new HashMap<Integer, ContextualManipulation>();
		mapdcm.put(0, dcm);
		ContextManager cm = ContextManager.getInstance();
		cm.setContextualManipulations(mapdcm);
		
		
		LancamentoCreate focus = new LancamentoCreate(l);
		cm.addFocus(focus);
		executeRules(focus);
		
		/*LancamentoCreate focus2 = new LacamentoCreate(); 
        Lancamento l2 =  lanc.getById((long)1);
        l2.setValor(2000.0);
		*/
        
        Categoria categoria = new Categoria();
        categoria.setIdCategoria((long)2);
        
		/*
		LancamentoCreate focus3 = new LacamentoCreate(); 
        Lancamento l3 =  lanc.getById((long)1);
        l3.setCategoria(categoria);
        l3.setValor(400.0);
        */
		cm.terminate();
	}

	
	private static void executeRules(LancamentoCreate focus) {
		ContextualBehavior contextualBehavior = new ContextualBehavior();
		contextualBehavior.setFocus(focus);
		try {
			contextualBehavior.execute();
		} catch (KnowledgeSessionNotCreatedException e) {
			e.printStackTrace();
		}
	}
	
    private static StatefulKnowledgeSession createStatefulSession() {
    	try {
	        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
	        kbuilder.add(ResourceFactory.newClassPathResource("knowledgebase-config.xml"), ResourceType.CHANGE_SET);
	        KnowledgeBuilderErrors errors = kbuilder.getErrors();
	        if (errors.size() > 0) {
	            for (KnowledgeBuilderError error : errors) {
	                System.err.println(error);
	            }
	            throw new IllegalArgumentException("Could not parse knowledge.");
	        }
	        KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
	        knowledgeBase.addKnowledgePackages(kbuilder.getKnowledgePackages());
	        return knowledgeBase.newStatefulKnowledgeSession();
    	} catch (Exception e) {
    		e.printStackTrace();
			return null;
		}
    }

}
